/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data;


import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author laptop24h
 */
public class Store {
    private int index=0;
    private int storeSize;
    private Queue<String> store = new LinkedList<String>();
    ThreadPoolExecutor threadpool;

    public Store(int storeSize) {
        if (storeSize <= 0) {
            throw new IllegalArgumentException("Size is illegal");
        }
        this.storeSize = storeSize;
    }

    public synchronized boolean isFull() {
        return store.size() == storeSize;
    }

    public synchronized boolean isEmpty() {
        return store.isEmpty();
    }

    public synchronized void put(String run) {
        while (isFull()) {
            System.out.println("store is full");
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Store.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        index++;
        store.add(run+":  "+index);
        System.out.println("Store receives cake " + run+":  "+index);
        notifyAll();
    }

    public synchronized String get() {
        String cakeName = null;
        while (isEmpty()) {
            System.out.println("There is no cake in Store");
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Store.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        cakeName = store.remove();
        notifyAll();
        return cakeName;
    }
}
